package com.shudingke.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shudingke.domain.*;
import com.shudingke.utils.FileNameUtils;
import com.shudingke.utils.MyPageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-06-29
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    StringRedisTemplate redisTemplate;
    
    
    
    @RequestMapping("/alogin")
    @ResponseBody
    public Integer alogin(String aname,String password){
        if (StringUtils.isEmpty(aname)){
            return 0;
        }else {
            String s = redisTemplate.opsForValue().get(aname);
            if (StringUtils.isEmpty(s)){
            Admin admin = new Admin();
            QueryWrapper<Admin> qw = new QueryWrapper<>();
            qw.eq("aname",aname);
            qw.eq("password",password);
            
            Admin admin1 = admin.selectOne(qw);
            if (admin1 != null){
                redisTemplate.opsForValue().set(aname,password);
                return 1;
            }else {
                return 0;
            }
            }else {
                if (s.equals(password)){
                    return 1;
                }else {
                    return 0;
                }
            }
        }
        
        
    }
    
    //首页
    @RequestMapping("/gotoAdminMain")
    public String gotoAdminMain(){
        return "adminMain";
    }

   
    @RequestMapping("/gotoList1")
    public String gotoList1(){
        return "list1";
    }


    @RequestMapping("/gotoList2")
    public String gotoList2(){
        return "list2";
    }

    @RequestMapping("/gotoList3")
    public String gotoList3(){
        return "list3";
    }

    @RequestMapping("/gotoList4")
    public String gotoList4(){
        return "list4";
    }

    @RequestMapping("/gotoList5")
    public String gotoList5(){
        return "list5";
    }

    @RequestMapping("/gotoList6")
    public String gotoList6(){
        return "list6";
    }

    @RequestMapping("/gotoList7")
    public String gotoList7(){
        return "list7";
    }

    @RequestMapping("/gotoList8")
    public String gotoList8(){
        return "list8";
    }

    @RequestMapping("/gotoUpdate1")
    public String gotoUpdate1(String uid, HttpServletRequest req){
        req.getSession().setAttribute("uid",uid);
        return "update1";
    }

    @RequestMapping("/gotoUpdate2")
    public String gotoUpdate2(String pid, HttpServletRequest req){
        req.getSession().setAttribute("pid",pid);
        return "update2";
    }

    @RequestMapping("/gotoAdd1")
    public String gotoAdd1(){
        return "add1";
    }

    @RequestMapping("/gotoAdd2")
    public String gotoAdd2(){
        return "add2";
    }
    
    
    
    //用户列表
    @RequestMapping("/userList")
    @ResponseBody
    public PageInfo<User> userList(){
        List<User> list = null;
        User user = new User();
        PageHelper.startPage(1,5);
        list = user.selectAll();
        PageInfo<User> info = new PageInfo<>(list);
        return info;
    }
    
    //用户查询和分页
    @RequestMapping("/userFindByCase")
    @ResponseBody
    public Map userFindByCase(@RequestParam(defaultValue = "1") String page,
                                 String uid){
        Map map = new HashMap();
        
        List<User> list = null;
        User user = new User();
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(uid)){
            qw.eq("uid",Integer.parseInt(uid));
            map.put("uid",uid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = user.selectList(qw);
        PageInfo<User> info = new PageInfo<>(list);
        map.put("info",info);
        return map;
    }

    //删除用户
    @RequestMapping("/userDelete")
    @ResponseBody
    public String userDelete(String uid){
        User user = new User();
        user.setUid(Integer.parseInt(uid));
        user.setIsDelete("是");
        user.updateById();
        return "111";
    }

    //修改用户初始化
    @RequestMapping("/userUpdateLoad")
    @ResponseBody
    public User userUpdateLoad(HttpServletRequest req){
        String uid = (String) req.getSession().getAttribute("uid");
        User user = new User();
        user.setUid(Integer.parseInt(uid));
        User byId = user.selectById();
        
        return byId;
    }

    //上传头像
    //MultipartFile文件上传类型
    @RequestMapping("/userFileUpload")
    public String userFileUpload(MultipartFile[] upload, HttpServletRequest req) throws Exception {
        String uuid = req.getSession().getAttribute("uid").toString();
        /*
         * 1.指定上传文件的目录
         * 2.检查该目录是否存在
         * 3.获取文件名(改上传文件名)20220623xxxxx
         * 4.上传
         * */

        //指定上传目录
        String realPath = "D:\\JAVA\\maven\\desktopStoreSystem\\target\\classes\\static\\images\\index\\";


        //检查上传目录是否存在
        File file = new File(realPath);
        if(!file.exists()) {
            file.mkdir();
        }


        for(MultipartFile mf : upload) {
            if(mf.getSize()>0) {
                //改变原始文件名
                String oldName = mf.getOriginalFilename();
                String newName = FileNameUtils.changeName(oldName);
                //上传
                mf.transferTo(new File(realPath + uuid + newName));
                User user = new User();
                user.setUid(Integer.parseInt(uuid));
                user.setAvatar(uuid + newName);
                user.updateById();
            }
        }

        //
        return "update1";
    }

    //修改用户
    @RequestMapping("/userUpdate")
    @ResponseBody
    public User userUpdate(@RequestBody User user,HttpServletRequest req){
        System.out.println(user);
        user.updateById();
        return user;
    }

    //添加用户
    @RequestMapping("/userAdd")
    @ResponseBody
    public User userAdd(@RequestBody User user,HttpServletRequest req){
        user.insert();
        return user;
    }






    
    
    
    
    
    
    



    //产品列表
    @RequestMapping("/productList")
    @ResponseBody
    public Map productList(){
        List<Products> list = null;
        Products products = new Products();
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.eq("status","1");
        PageHelper.startPage(1,5);
        list = products.selectList(qw);
        PageInfo<Products> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //产品查询和分页
    @RequestMapping("/productFindByCase")
    @ResponseBody
    public Map productFindByCase(@RequestParam(defaultValue = "1") String page,
                              String pid){
        Map map = new HashMap();

        List<Products> list = null;
        Products products = new Products();
        QueryWrapper<Products> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pid) && !pid.equals("undefined")){
            qw.eq("id",Integer.parseInt(pid));
            map.put("id",pid);
        }
        qw.eq("status","1");
        PageHelper.startPage(Integer.parseInt(page),5);
        list = products.selectList(qw);
        PageInfo<Products> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除产品
    @RequestMapping("/productDelete")
    @ResponseBody
    public String productDelete(String pid){
        Products products = new Products();
        products.setId(Integer.parseInt(pid));
        products.setStatus("0");
        products.updateById();
        return "111";
    }


    //修改产品初始化
    @RequestMapping("/productUpdateLoad")
    @ResponseBody
    public Products productUpdateLoad(HttpServletRequest req){
        String pid = (String) req.getSession().getAttribute("pid");
        Products products = new Products();
        products.setId(Integer.parseInt(pid));
        Products byId = products.selectById();

        return byId;
    }


    //上传产品图片
    //MultipartFile文件上传类型
    @RequestMapping("/productFileUpload")
    public String productFileUpload(MultipartFile[] upload, HttpServletRequest req) throws Exception {
        String id = req.getSession().getAttribute("pid").toString();
        /*
         * 1.指定上传文件的目录
         * 2.检查该目录是否存在
         * 3.获取文件名(改上传文件名)20220623xxxxx
         * 4.上传
         * */

        //指定上传目录
        //先找到这个目录
        Products products = new Products();
        products.setId(Integer.parseInt(id));
        Products products1 = products.selectById();
        String realPath = products1.getImage();
        //但是这个路径是发布之前的路径
        String[] split = realPath.split("/");
        //这是最后一个文件夹的名称
        String name = split[split.length - 1];
        realPath = "D:\\JAVA\\maven\\desktopStoreSystem\\target\\classes\\static\\images\\" + name + "\\";


        //检查上传目录是否存在
        File file = new File(realPath);
        if(!file.exists()) {
            file.mkdir();
        }


        for(MultipartFile mf : upload) {
            if(mf.getSize()>0) {
                //上传   将原来的1_big.png覆盖掉
                mf.transferTo(new File(realPath + "1_big.png"));
            }
        }

        //
        return "update2";
    }

    //修改产品
    @RequestMapping("/productUpdate")
    @ResponseBody
    public Products productUpdate(@RequestBody Products products,HttpServletRequest req){
        System.out.println(products);
        products.updateById();
        return products;
    }

    //添加产品
    @RequestMapping("/productAdd")
    @ResponseBody
    public Products productAdd(@RequestBody Products products,HttpServletRequest req){
        products.insert();
        return products;
    }
    
    
    
    
    
    











    //订单列表
    @RequestMapping("/ordersList")
    @ResponseBody
    public Map ordersList(){
        List<Orders> list = null;
        Orders orders = new Orders();
        PageHelper.startPage(1,5);
        list = orders.selectAll();
        PageInfo<Orders> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //订单查询和分页
    @RequestMapping("/ordersFindByCase")
    @ResponseBody
    public Map ordersFindByCase(@RequestParam(defaultValue = "1") String page,
                                 String oid){
        Map map = new HashMap();

        List<Orders> list = null;
        Orders orders = new Orders();
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(oid) && !oid.equals("undefined")){
            qw.eq("oid",Integer.parseInt(oid));
            map.put("oid",oid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = orders.selectList(qw);
        PageInfo<Orders> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除订单
    @RequestMapping("/ordersDelete")
    @ResponseBody
    public String ordersDelete(String pid){

        return "111";
    }
    
    //订单发货
    @RequestMapping("/deliverGoods")
    @ResponseBody
    public String deliverGoods(String oid){
        Orders orders = new Orders();
        orders.setOid(Integer.parseInt(oid));
        orders.setStatus("已发货");
        orders.updateById();
        return "111";
    }





    
    



    //订单项目列表
    @RequestMapping("/orderItemList")
    @ResponseBody
    public Map orderItemList(){
        List<Orderitem> list = null;
        Orderitem orderitem = new Orderitem();
        PageHelper.startPage(1,5);
        list = orderitem.selectAll();
        PageInfo<Orderitem> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //订单项目查询和分页
    @RequestMapping("/orderItemFindByCase")
    @ResponseBody
    public Map orderItemFindByCase(@RequestParam(defaultValue = "1") String page,
                                String oid){
        Map map = new HashMap();

        List<Orderitem> list = null;
        Orderitem orderitem = new Orderitem();
        QueryWrapper<Orderitem> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(oid) && !oid.equals("undefined")){
            qw.eq("oid",Integer.parseInt(oid));
            map.put("oid",oid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = orderitem.selectList(qw);
        PageInfo<Orderitem> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除订单项目
    @RequestMapping("/orderItemDelete")
    @ResponseBody
    public String orderItemDelete(String pid){

        return "111";
    }












    //地区列表
    @RequestMapping("/districtList")
    @ResponseBody
    public Map districtList(){
        List<District> list = null;
        District district = new District();
        PageHelper.startPage(1,5);
        list = district.selectAll();
        PageInfo<District> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //地区查询和分页
    @RequestMapping("/districtFindByCase")
    @ResponseBody
    public Map districtFindByCase(@RequestParam(defaultValue = "1") String page,
                                   String did){
        Map map = new HashMap();

        List<District> list = null;
        District district = new District();
        QueryWrapper<District> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(did) && !did.equals("undefined")){
            qw.eq("id",Integer.parseInt(did));
            map.put("did",did);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = district.selectList(qw);
        PageInfo<District> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除地区
    @RequestMapping("/districtDelete")
    @ResponseBody
    public String districtDelete(String pid){

        return "111";
    }














    //购物车列表
    @RequestMapping("/cartList")
    @ResponseBody
    public Map cartList(){
        List<Cart> list = null;
        Cart cart = new Cart();
        PageHelper.startPage(1,5);
        list = cart.selectAll();
        PageInfo<Cart> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //购物车查询和分页
    @RequestMapping("/cartFindByCase")
    @ResponseBody
    public Map cartFindByCase(@RequestParam(defaultValue = "1") String page,
                                  String cid){
        Map map = new HashMap();

        List<Cart> list = null;
        Cart cart = new Cart();
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cid) && !cid.equals("undefined")){
            qw.eq("cid",Integer.parseInt(cid));
            map.put("cid",cid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = cart.selectList(qw);
        PageInfo<Cart> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除购物车
    @RequestMapping("/cartDelete")
    @ResponseBody
    public String cartDelete(String pid){

        return "111";
    }











    
    
    //收藏列表
    @RequestMapping("/collectionsList")
    @ResponseBody
    public Map collectionsList(){
        List<Collections> list = null;
        Collections collections = new Collections();
        PageHelper.startPage(1,5);
        list = collections.selectAll();
        PageInfo<Collections> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //收藏查询和分页
    @RequestMapping("/collectionsFindByCase")
    @ResponseBody
    public Map collectionsFindByCase(@RequestParam(defaultValue = "1") String page,
                              String cid){
        Map map = new HashMap();

        List<Collections> list = null;
        Collections collections = new Collections();
        QueryWrapper<Collections> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cid) && !cid.equals("undefined")){
            qw.eq("cid",Integer.parseInt(cid));
            map.put("cid",cid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = collections.selectList(qw);
        PageInfo<Collections> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除收藏
    @RequestMapping("/collectionsDelete")
    @ResponseBody
    public String collectionsDelete(String pid){

        return "111";
    }















    //目录列表
    @RequestMapping("/categoryList")
    @ResponseBody
    public Map categoryList(){
        List<Category> list = null;
        Category category = new Category();
        PageHelper.startPage(1,5);
        list = category.selectAll();
        PageInfo<Category> info = new PageInfo<>(list);
        List pageList = MyPageList.getPageList("1",info);
        Map map = new HashMap();
        map.put("info",info);
        map.put("pageList",pageList);
        return map;
    }

    //目录查询和分页
    @RequestMapping("/categoryFindByCase")
    @ResponseBody
    public Map categoryFindByCase(@RequestParam(defaultValue = "1") String page,
                                     String cid){
        Map map = new HashMap();

        List<Category> list = null;
        Category category = new Category();
        QueryWrapper<Category> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cid) && !cid.equals("undefined")){
            qw.eq("id",Integer.parseInt(cid));
            map.put("cid",cid);
        }
        PageHelper.startPage(Integer.parseInt(page),5);
        list = category.selectList(qw);
        PageInfo<Category> info = new PageInfo<>(list);
        map.put("info",info);

        List pageList = MyPageList.getPageList(page,info);
        map.put("pageList",pageList);
        return map;
    }

    //删除目录
    @RequestMapping("/categoryDelete")
    @ResponseBody
    public String categoryDelete(String pid){

        return "111";
    }
}
